package com.chen.ga.fitness;

import com.chen.ga.Fitness;
import com.chen.webservice.Utils.CoverHandler;
import com.chen.webservice.Utils.RuleHandler;
import com.chen.webservice.model.MyOrder;

import java.util.List;

import static com.chen.webservice.Utils.RuleChain.ruleHandleChain;

/**
 * @description: 适应度函数，具体适应度规则在ruleChain指定
 *
 * @params:
 * @return:
 * @author: chenzhiwen
 * @dateTime: 2021/10/21 下午7:15
 */
public class MyVectorFitness implements Fitness<MyOrder, Double> {

    private static final CoverHandler coverHandler = new CoverHandler();

    @Override
    public Double calculate(MyOrder chromosome, List<Integer> rules) {
        double delta = 0;

        int ruleRate = 0;

        for (RuleHandler ruleHandler : ruleHandleChain) {
            if (ruleHandler.ruleNum() == coverHandler.ruleNum()) {
                ruleRate += coverHandler.checkResultWithCover(chromosome.getOrders(), chromosome.getCovOrders(),
                        chromosome.getOriOrders(), chromosome.getStartTime());
            } else if (ruleHandler.isMatch(rules)) {
                ruleRate += ruleHandler.checkResult(chromosome.getOrders(), ruleRate);
            }
        }

        return delta + ruleRate;
    }

}
